package com.bj.dz.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.bj.dz.service.RoleMenuService;
import org.springframework.stereotype.Service;

import com.bj.dz.mapper.RoleMenuMapper;
import com.bj.dz.entity.RoleMenu;

@Service("roleMenuService")
public class RoleMenuServiceImp implements RoleMenuService {
	@Resource
	private RoleMenuMapper rmd;

	
	
	//�����ɫ��Ӧ��Ȩ��(�˵�)
	@Override
	public boolean saveRoleMenus(RoleMenu rm) {
		Integer rid=rm.getRid();
		//�Ȳ�ѯ�ý�ɫ��Ӧ��Ȩ�޲˵�(����û�в˵�)
		List<RoleMenu> rmList=findRoleMenusByRid(rid);
		boolean flag=rmList.isEmpty();//�жϼ����Ƿ�Ϊ��
		if(!flag){
			//��ɾ����ǰ�ý�ɫ��Ӧ��Ȩ�޲˵�
			int num0=rmd.delRoleMenusByRid(rid);
			if(num0<=0){
				return false;
			}
		}
		
		//�������в˵���ŵ��ַ���
		String menuNums=rm.getMenuNum();
		String [] mns=menuNums.split("&");
		for (String menuNum : mns) {
			RoleMenu rmu=new RoleMenu(rid,menuNum,null);
			int num=rmd.saveRoleMenus(rmu);
			if(num<=0){
				return false;
			}
		}
		
		return true;
	}

	
	//ͨ����ɫid��ѯ��Ӧ��Ȩ��(�˵�)
	@Override
	public List<RoleMenu> findRoleMenusByRid(Integer rid) {
		List<RoleMenu> rmList=rmd.findRoleMenusByRid(rid);
		if(rmList==null){
			rmList=new ArrayList<RoleMenu>();
		}
		return rmList;
	}
	
	
}
